package com.yq.spbYq.controller;

import com.yq.spbYq.WasteRecord;
import com.yq.spbYq.service.WasteRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class WasteRecordController {
    @Autowired
    private WasteRecordService wasteRecordService;
    @RequestMapping("/selectWasteRecord")
    public List<WasteRecord> selectWasteRecord()
    {
        List<WasteRecord> wasteRecord = wasteRecordService.selectWasteRecord();
        if(wasteRecord!=null)
            System.out.println(wasteRecord);
        else
            System.out.println("查询失败");
        return wasteRecord;
    }
    @GetMapping("/selectWasteRecordByResidentId")
    public List<WasteRecord> selectWasteRecordByResidentId(Long residentId)
    {
        List<WasteRecord> wasteRecord = wasteRecordService.selectWasteRecordByResidentId(residentId);
        if(wasteRecord!=null)
            System.out.println(wasteRecord);
        else
            System.out.println("查询失败");
        return wasteRecord;
    }
    // 添加新的 WasteRecord
    @PostMapping("/addwasteRecord")
    public WasteRecord addWasteRecord(@RequestBody WasteRecord wasteRecord) {
        return wasteRecordService.addWasteRecord(wasteRecord);
    }
    //根据居民id删除 WasteRecord
    @GetMapping("/deleteWasteRecordByResidentId")
    public void deleteWasteRecordByResidentId(Long residentId)
    {
        wasteRecordService.deleteWasteRecord(residentId);
    }
    //修改 WasteRecord
    @PostMapping("/updateWasteRecord")
    public WasteRecord updateWasteRecord(@RequestBody WasteRecord wasteRecord)
    {
        deleteWasteRecordByResidentId(wasteRecord.getResidentId());
        return wasteRecordService.addWasteRecord(wasteRecord);
    }
}
